package com.yycx.module.bus.provider.handler.haiying.settle.apply;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.client.enums.SettleTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 结算单保存扩展器
 *
 * @author zyf
 */
@Component
public class HySettleApplyPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private BusHyorderReturnService orderDetailsService;

    @Resource
    public RedisUtils redisUtils;

    @Autowired
    private BusinessUtils businessUtils;

    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private BusDeptService deptService;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        //结算范围
        Integer settleType = entityMap.getInt("settleType", 1);
        //结算数据
        String selectIds = entityMap.get("selectIds");
        BigDecimal totalSettleAmount = orderDetailsService.totalSettleApplyAmount(selectIds, settleType);
        EntityMap saleManMap = orderDetailsService.countSettleManId(selectIds, settleType);
        if (FlymeUtils.isNotEmpty(saleManMap)) {
            String drugType = saleManMap.get("drugType");
            payment.setDrugType(drugType);
            Long manId = saleManMap.getLong("manId");
            BusSaleman saleman = salemanService.getById(manId);
            if (FlymeUtils.isNotEmpty(saleman)) {
                payment.setSaleManId(manId);
                payment.setPayeeName(saleman.getSaleManName());
                Integer saleManType = FlymeUtils.getInteger(saleman.getSaleManType(), 1);
                if (saleManType.equals(1)) {
                    payment.setSaleManCode(saleman.getSaleManBusinessNo());
                } else {
                    payment.setSaleManCode(saleman.getHeadCode());
                }
            }
            Long deptId = saleManMap.getLong("deptId");
            if (FlymeUtils.isNotEmpty(deptId)) {
                BusDept dept = deptService.getById(deptId);
                if (FlymeUtils.isNotEmpty(dept)) {
                    payment.setDeptName(dept.getDeptName());
                }
            }

        }
        String title = SettleTypeEnum.getName(settleType);
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        payment.setTitle(title);
        payment.setAmount(totalSettleAmount);
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setOrganizationId(companyId);
        payment.setCompanyId(companyId);
        payment.setPrintTag(CommonConstants.INT_0);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setSettleType(settleType);
        payment.setAmountType(AmountTypeEnum.SettleApply.getCode());
        String paymentNo = businessUtils.getPaymentNo(companyId, userId, AmountTypeEnum.SettleApply);
        payment.setPaymentNo(paymentNo);
        payment.setPaymentState(CommonConstants.INT_0);
        payment.setSelectIds(selectIds);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String settleIds = cs.getParams("selectIds");
        Integer settleType = params.getInt("settleType", 1);
        String[] settleIdArray = settleIds.split(",");
        List<Long> list = new ArrayList<>();
        for (String orderDetailsId : settleIdArray) {
            BusHyorderReturn orderDetail = orderDetailsService.getById(orderDetailsId);
            Long settleApplyId = orderDetail.getSettleApplyId();
            Long settleApplyIdC = orderDetail.getSettleApplyIdC();
            Long settleApplyIdD = orderDetail.getSettleApplyIdD();
            Long settleApplyIdE = orderDetail.getSettleApplyIdE();
            BigDecimal saleAmount = FlymeUtils.getBigDecimal(orderDetail.getSettleAmount(), "0");
            BigDecimal saleAmountC = FlymeUtils.getBigDecimal(orderDetail.getSettleAmountC(), "0");
            BigDecimal saleAmountD = FlymeUtils.getBigDecimal(orderDetail.getSettleAmountD(), "0");
            BigDecimal saleAmountE = FlymeUtils.getBigDecimal(orderDetail.getSettleAmountE(), "0");
            Boolean tag = false;
            if (settleType.equals(1) && FlymeUtils.isEmpty(settleApplyId) && FlymeUtils.nezero(saleAmount)) {
                tag = true;
            }
            if (settleType.equals(2) && FlymeUtils.isEmpty(settleApplyIdD) && FlymeUtils.nezero(saleAmountD)) {
                tag = true;
            }
            if (settleType.equals(3) && FlymeUtils.isEmpty(settleApplyIdC) && FlymeUtils.nezero(saleAmountC)) {
                tag = true;
            }
            if (settleType.equals(4) && FlymeUtils.isEmpty(settleApplyIdE) && FlymeUtils.nezero(saleAmountE)) {
                tag = true;
            }
            if (tag) {
                list.add(orderDetail.getOrderDetailsReturnId());
            }
        }
        if (FlymeUtils.isNotEmpty(list)) {
            //关联结算申请ID
            orderDetailsService.updateSettleApplyId(entity.getPaymentId(), list, settleType);
        }

    }
}
